package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.dto.DishDto;
import com.itheima.entity.Dish;
import com.itheima.entity.DishFlavor;
import com.itheima.entity.Setmeal;
import com.itheima.entity.SetmealDish;
import com.itheima.mapper.DishMapper;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishService dishService;

    /*
    新增菜品需要同时操作两张表：dish，dish_flavor
    */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品基本信息
        this.save(dishDto);
        //获取口味信息，注意目前口味信息中没有id值
        List<DishFlavor> flavors = dishDto.getFlavors();
        //获取dishDto中的id值
        Long flavorid = dishDto.getId();
        //使用steam流遍历，并将id值赋值给口味数据中的id
        flavors = flavors.stream().map((iteam) -> {
            iteam.setDishId(flavorid);
            return iteam;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    /*
    修改菜品信息同时操作两张表dish，dish_flavor
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基础信息
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        //查询当前菜品对应口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavorslist = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavorslist);
        return dishDto;
    }

    /*
    修改菜品信息：同时操作两张表
     */
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //更新菜品基本信息
        this.updateById(dishDto);
        //删除当前菜品口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //添加当前菜品口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        Long flavorid = dishDto.getId();
        //使用steam流遍历，并将id值赋值给口味数据中的id
        flavors = flavors.stream().map((iteam) -> {
            iteam.setDishId(flavorid);
            return iteam;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    //批量启售停售
    @Override
    public void updatestatus(Integer status, Long ids) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ids != null, Dish::getId, ids);
        List<Dish> dishlist = dishService.list(queryWrapper);
        dishlist = dishlist.stream().map((iteam)->{
            iteam.setStatus(status);
            dishService.updateById(iteam);
            return iteam;
        }).collect(Collectors.toList());
    }
    /*
    批量删除
     */
    @Override
    public void removeWithDishFlavor(List<Long> ids) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        queryWrapper.eq(Dish::getStatus,1);
        int count = this.count(queryWrapper);
        if (count >0) {
            //菜品正在启售不能删除
            throw new CustomException("菜品正在启售中，不能删除");
        }
        //删除第一个菜品表
        this.removeByIds(ids);
        //删除第二个菜品口味关系表
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(lambdaQueryWrapper);
    }
}